package com.etc.lock;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: knowno
 * @create: 2025-09-19 15:49
 * @Version 1.0
 **/
public class TestLock {

    public static void main(String[] args) throws InterruptedException {
        TestLock testLock = new TestLock();
        testLock.test();
    }
    ReentrantLock lock = new ReentrantLock();
    public void test() throws InterruptedException {
        List<Object> objects = new ArrayList<>();
        //循环
        for (int i = 1; i <= 50000; i++) {
            //创建线程对象 ->每循环一次，将这个线程对象name添加到list中
            new Thread(() ->
            {
                lock.lock();
                //多线程情况下，如果有某一个线程进入了这段的时候，其他线程进不来.
                objects.add(Thread.currentThread().getName());
                lock.unlock();
            }
            ).start();
        }
        //加一个延迟
        Thread.sleep(20000);
        System.out.println(objects.size());
    }
}
